<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_user_status {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT * FROM tbl_user_status";
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function save($user_status_id, $first_name, 
                $last_name, $phone_number, $email, $password, $role_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        if ($user_status_id == 0) {
            $query = "INSERT INTO tbl_user_status(email, password, role_id,
                first_name, last_name, phone_number) VALUES (
                '" . $email . "','" . md5($password) . "'," . $role_id . ",'" . $first_name . "','" . $last_name . "','" . $phone_number . "')";
        } else {
            $query = "UPDATE tbl_user_status SET "
                    . "email = '" . $email . "',"
                    . "password = '" . $password . "',"
                    . "role_id = " . $role_id . ","
                    . "first_name = '" . $first_name . "',"
                    . "last_name = '" . $last_name . "',"
                    . "phone_number = '" . $phone_number . "' "
                    . "WHERE id = " . $user_status_id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user_status";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_user_status WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_first() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user_status "
                . "LIMIT 0, 1";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }
    
}
